/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 * */

// AmzServer - Tototoy's server model.
// Author: tonyjobmails@gmail.com (tonyhack).
//
// Define class UDPCredibleStreamSocketCommunicator.
//
// UDPCredibleStreamSocketCommunicator provide a credible udp communication.
//

#ifndef __UDP__CREDIBLE__STREAM__SOCKET__COMMUNICATOR__H
#define __UDP__CREDIBLE__STREAM__SOCKET__COMMUNICATOR__H

#include "udp_stream_socket_communicator.h"
#include "udp_credible_pattern_receiver.h"
#include "udp_credible_pattern_sender.h"

namespace amz {

class UDPCredibleStreamSocketCommunicator : public UDPStreamSocketCommunicator {
 public:
  UDPCredibleStreamSocketCommunicator(const sockaddr_in &address,
         int len, UDPSocket *udp_socket, CompressType compress,
         EncryptType encrypt, uint32 max_serial_number, uint8 resend_times,
         uint32 timeout_msecs, int flag = 0);
  virtual ~UDPCredibleStreamSocketCommunicator();

  // Check send.
  inline virtual bool CanSend() const {
    return this->sender_.Available();
  }

  // Send message to socket.
  virtual int Send();

  // After receiving any data, dispatch to the buffer(according to address).
  virtual int PushReceive(char *udp_credible_packet, int size);

  // There Should implement timeout resending and receiving complete to ordering receiving.
  virtual void CredibleTimeoutResend();
  virtual void CredibleOrderReveive();

 protected:
  UDPCrediblePatternReceiver receiver_;
  UDPCrediblePatternSender sender_;

  SimpleByteBufferDynamic buffer_;
};

}  // namespace amz

#endif  // __UDP__CREDIBLE__STREAM__SOCKET__COMMUNICATOR__H
